import 'package:flutter/material.dart';

/// Navigator工具类
class NavigatorUtil {
  static void pop<T>(BuildContext context, [T result]) {
    Navigator.pop(context, result);
  }

  static void push<T>(BuildContext context, Widget scene,
      [ValueSetter<T> callback]) {
    var future = Navigator.push(
      context,
      MaterialPageRoute(
        builder: (BuildContext context) => scene,
      ),
    );
    if (callback != null) {
      future.then((result) {
        callback(result);
      }).catchError((error) {
        debugPrint('$error');
      });
    }
  }

  /// 替换页面 当新的页面进入后，之前的页面将执行dispose方法
  static void pushReplacement(BuildContext context, Widget scene) {
    Navigator.pushReplacement(
        context,
        MaterialPageRoute(
          builder: (BuildContext context) => scene,
        ));
  }

  /// 指定页面加入到路由中，然后将其他所有的页面全部pop
  static void pushAndRemoveUntil(BuildContext context, Widget scene) {
    Navigator.pushAndRemoveUntil(
        context,
        MaterialPageRoute(
          builder: (BuildContext context) => scene,
        ),
        (route) => route == null);
  }
}
